#ifndef SHIPMANAGEB_H
#define SHIPMANAGEB_H

#pragma once

/* **********************************************************************************************
 *
 * 船舶数据从串口获取
 *
 *
 * ******************************************************************************************** */

#include "ShipManage.h"
#include <QThread>
#include <QElapsedTimer>
#include "MsgStruct.h"

namespace ns_yht
{
    class ShipManageB : public ShipManage
    {
        Q_OBJECT

    public:
        ~ShipManageB();
        static void newInstance();

        void startThread(QThread &thread) override;
        bool isBusy() const override;

    public slots:
        void searchOneShip(quint64 mmsi, int panelId = 0) override;
        void searchShipWithKeyword(const QString &keyword, int panelId = 0) override;
        void searchTrack(quint64 mmsi, const QString &startTime, const QString &endTime) override;

        void onRadiusChanged(double radius_nm) override;

    private slots:
        void onAis1Finished(AIS_1 *ais, bool vdo);
        void onAis4Finished(AIS_4 *ais, bool vdo);
        void onAis5Finished(AIS_5 *ais, bool vdo);

        void onAis18Finished(AIS_18 *ais, bool vdo);
        void onAis24Finished(AIS_24 *ais, bool vdo);

        void onHDTFinished(double head);
        void onHDGFinished(HDG *hdg);
        void onGGAFinished(GGA *gga);
        void onGLLFinished(GLL *gll);
        void onRMCFinished(RMC *rmc);
        void onVTGFinished(VTG *vtg);
        void onGNSFinished(GNS *gns);

        void onDPTFinished(DPT *dpt);

        void onposStateChange(int state);

    signals:
        void portStateChanged(QString portName, bool isOpened);

    private:
        ShipManageB();

        void request() override;
        void updateShips();
        void onOwnShipChanged();
        // 默认更新距离，仅当对刚刚获取的ARPA调用该方法时不需要计算距离,是否为新增船舶
        void updateShip(QSharedPointer<ModelShip> ship, bool refreshDistance = true, bool isAdd = true);

        void updateAlertArea();

        QSharedPointer<ModelShip> getOrNewShip(quint64 mmsi, bool vdo, bool &isNewShip);
        void processExpiredShips();
        void cancelCollision(quint64 mmsi);

        bool m_isBusy{false};

        int m_filterMode{0};
        double m_dcpaLimit{1852.0}; // 米
        double m_tcpaLimit{10.0};   // 分钟

        double m_left{100.0};
        double m_right{100.0};
        double m_front{500.0};
        double m_back{100.0};
    };

} // namespace ns_yht

#endif
